#include<stdio.h>
#include<stdlib.h>
#include<strings.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
//#include<arpa/inet.h>
#include<linux/in.h>

#define PORT 9200
#define HOST "127.0.0.1"

int main(int argc, char *argv[]){
  int fd;
  struct sockaddr_in server_addr;
  int err;

  fd = socket(AF_INET,SOCK_STREAM,0);
  if(fd<0){
    perror("socket error\n");
    exit(1);
  }
  bzero(&server_addr,sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(PORT);
  server_addr.sin_addr.s_addr = INADDR_ANY;

  inet_pton(AF_INET, HOST, &server_addr.sin_addr);

  connect(fd,(struct sockaddr*)&server_addr, sizeof(struct sockaddr));
  process_conn(fd);
  close(fd);
}

void process_conn(int fd){
  ssize_t size = 0;
  char buffer[1024];
  while (1) {
    size = read(0,buffer,1024);
    if(size>0){
      write(fd,buffer,size);
      size = read(fd,buffer,1024);
      write(1,buffer,size);
    }
  }
}
